<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
/**
 * @author de Flotte Maxence <xamax87@gmail.com>
 * @package std
 * @version 1.0
 */
/**
 * Objet puZZle permettant la construction d'un puZZle
 * @package std
 */
class std_Page extends Bout {
	protected $title = "puZZle Project";
	private $mainCSS;
	private $css;
	private $js;
	private $style;
	private $noStructure = false;
	public $noCache = false;

/**
 * Constructeur
 * Cette classe a un constructeur spécifique. En effet, le second argument est obsolète puisqu'il s'agirait d'un pointeur vers elle-même
 * @param int $name nom de l'objet puZZle
 */
	public function __construct($name) {
		$this->name = $name;
	}
/**
 * Change le titre de la page
 * @param string $title titre de la page
 * @return void
 */
	public function setTitle($title) {
		$this->title = $title;
	}
/**
 * Defini si il faut retourner ou non la structure minimale d'une page html (<head>,<body>,...)
 * @param string $noStructure true | false
 * @return void
 */
	public function setNoStructure($noStructure) {
		if(strtolower($noStructure) == 'true' or strtolower($noStructure) == '1' or strtolower($noStructure) == 'yes')
			$this->noStructure = true;
		else
			$this->noStructure = false;
	}
/**
 * Defini le css principal de la page
 * @param string $css Nom du fichier css contenu dans le dossier ./ZzCSS
 * @return void
 */
	public function setMainCSS($css) {
		$this->mainCSS = '<link rel="stylesheet" media="screen, handheld, tv, projection" title="'.preg_replace('!^(.*\/)*(\w+)\..+$!','$2', $css).'" href="./ZzCSS/'.$css.'"/>';
	}
/**
 * Defini un nouveau css
 * @param string $css Nom du fichier css contenu dans le dossier ./ZzCSS
 * @return void
 */
	public function setCSS($css) {
		$this->css .= "\n".'		<link rel="stylesheet" media="screen, handheld, tv, projection" href="./ZzCSS/'.$css.'"/>';
	}
/**
 * Defini un nouveau css
 * @param string $css Nom du fichier css contenu dans le dossier ./ZzCSS
 * @return void
 */
	public function addCSS($css) {
		$this->setCSS($css);
	}
/**
 * Defini un nouveau script JS
 * @param string $js Nom du fichier js contenu dans le dossier ./ZzScript
 * @return void
 */
	public function setJS($js) {
		$this->js .= "\n".'		<script type="text/javascript" src="./ZzScript/'.$js.'" ></script>';
	}
/**
 * Defini un nouveau script JS
 * @param string $js Nom du fichier js contenu dans le dossier ./ZzScript
 * @return void
 */
	public function addJS($js) {
		$this->setJS($js);
	}
/**
 * Ajoute des styles
 * @param string $Nom Nom du style
 * @param string $Info Valeur du style
 * @return void
 */
	public function addSTYLE($Nom,$Info) {
		$this->style[$Nom] = '	'.str_replace(";",";\n",$Info);
	}

	public function css() {
		if(isset($this->_Flux))
			foreach($this->_Flux as $flux)
				$return .= '<link rel="stylesheet" media="'.$flux['type'].'" type="text/css" title="'.preg_replace('!^(.*\/)*(\w+)\..+$!','$2', $flux['flux']).'" href="'.$flux['flux'].'" />';
			return $return;
	}

	/*public function buildStaticBout() {
		if(isset($this->codeBuilt))
			return $this->codeBuilt;
		else {
			$this->buildStaticListObject();
			if(method_exists($this,'buildStatic'))
				return $this->codeBuilt = $this->buildStatic().$this->Me();
			else
				return $this->codeBuilt = $this->build();
		}
	}*/
/**
 * Retourne le code html
 * @return string
 */
	public function build() {
		$Style = null;
		if(isset($this->style))
			foreach($this->style as $Nom => $Info)
		$Style .= '			'.$Nom.' {
'.'			'.str_replace("\n","\n				",$Info).'
			}
';
		if(is_array($GLOBALS['js']))
			foreach($GLOBALS['js'] as $js => $val)
				$this->addJS($js.'.js');

		if(!$this->noStructure)
			$retour = '<!DOCTYPE html>
<html>
<!-- generate with puZZle Project v1.5 -->
	<head>
		<title>'.$this->title.'</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<!-- CSS -->
		'.$this->mainCSS.$this->css.'
		
		<!-- Query Window -->
		<link href="./ZzScript/windowsjs/themes/default.css" rel="stylesheet" type="text/css"/>
		<link href="./ZzScript/windowsjs/themes/alert.css" rel="stylesheet" type="text/css"/>
		<link href="./ZzScript/windowsjs/themes/alphacube.css" rel="stylesheet" type="text/css"/>
		<script type="text/javascript" src="./ZzScript/prototype.js"> </script>
		<script type="text/javascript" src="./ZzScript/windowsjs/javascripts/effects.js"> </script>
		<script type="text/javascript" src="./ZzScript/windowsjs/javascripts/window.js"> </script>
		<script type="text/javascript" src="./ZzScript/windowsjs/javascripts/debug.js"> </script>
		<script type="text/javascript" src="./ZzScript/main.js"> </script>
		
		<!-- JS Script -->
		'.$this->js.'
		
		<style type="text/css">
'.$Style.'		</style>
	</head>
	<body>
		<script type="text/javascript">
		<!--
		
		function LoadPage(Site,id) {
			var xhr;
			if (window.XMLHttpRequest) xhr = new XMLHttpRequest(); // Firefox, Opera, Konqueror, Safari, ...
			else if (window.ActiveXObject) xhr = new ActiveXObject(\'Microsoft.XMLHTTP\'); // Internet Explorer
			else alert(\'JavaScript : votre navigateur ne supporte pas les objets XMLHttpRequest...\'); // Navigateur ancien
			
			xhr.open(\'GET\',Site,true);

			//On met le logo de chargement
			//if (document.getElementById) document.getElementById(id).innerHTML = "Chargement";

			xhr.onreadystatechange = function() {
				if (xhr.readyState == 4) {
					if (document.getElementById) document.getElementById(id).innerHTML = xhr.responseText;
				}
			}
			
			xhr.setRequestHeader(\'Content-Type\',\'application/x-www-form-urlencoded\');
			
			//On envoi la requête
			xhr.send(null);
		}

		function reLoad(id,arg) {
			var obj;
			obj = document.getElementById(id).getAttribute("obj");
			LoadPage(\'./loadZZO.php'.ZzGetUrl::with().'&name=\'+id+arg+\'&ZZO=\'+obj,id);
		}

		function reLoadBoot(action,id) {
			arg = document.getElementById(id).getElementsByTagName("arg")[0].getAttribute("arg");
			reLoad(id,arg);
		}

		function addDeclencheur(id,e,arg) {
			alert("\'"+arg+"\'");
			document.getElementById(id).setAttribute(e,arg);
			alert("ee");
		}

		//LoadPage(\'affichage.html?info=gr\',\'google\');
		//setInterval("LoadPage(\'affichage.php?info=gr\',\'google\')",1000);

		//-->
		</script>
'.$this->inner.'
	<button onClick="openWindowQuery()" type="button">ajax Query</button>
	</body>
</html>';
		else
			$retour = $this->inner;

		return $retour;
	}
/**
 * Lance la construction du puZZle dynamique
 * @return string
 */
	public function buildpuZZle() {
		return $this->buildBout();
	}
/**
 * Lance la construction du puZZle statique
 * @return string
 */
	public function buildStaticpuZZle() {
		return $this->buildStaticBout();
	}
}
?>